local shanzhi = fk.CreateSkill{
    name = "ym1__shanzhi",
}
Fk:loadTranslationTable{
    ["ym1__shanzhi"] = "擅执",
    [":ym1__shanzhi"] = "当你使用伤害牌指定唯一目标时，可令其选择X项：1.交给你一张牌；2.此牌伤害基数+1；3.令你移动一张“权”。（X为你们武将牌上的牌数差）",
    ["#ym1__shanzhi"] = "擅执：你可以令%dest选择%arg项并执行",
    ["#ym1__shanzhi-choice1"] = "交给%dest一张牌",
    ["#ym1__shanzhi-choice2"] = "此%arg伤害基数+1",
    ["#ym1__shanzhi-choice3"] = "令%dest移动一张“权”",
    ["#ym1__shanzhi-choice0"] = "擅执：移动一张“权”",

    ["$ym1__shanzhi1"] = "无我手令，汝意欲何为？",
    ["$ym1__shanzhi2"] = "朝中文书，需先经于我手。",
}
shanzhi:addEffect(fk.AfterCardTargetDeclared,{
    can_trigger = function(self, event, target, player, data)
        if target == player and player:hasSkill(shanzhi.name) and data:isOnlyTarget(data.tos[1]) and
        data.card.is_damage_card then
            local room = player.room
            local to = room:getPlayerById(data.tos[1].id)
            local cards1,cards2 = 0,0
            for _, ids in pairs(player.special_cards) do
                cards1 = cards1 + #ids
            end
            for _, ids in pairs(to.special_cards) do
                cards2 = cards2 + #ids
            end
            local n = math.abs(cards1-cards2)
            return n > 0
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local to = room:getPlayerById(data.tos[1].id)
        local cards1,cards2 = 0,0
        for _, ids in pairs(player.special_cards) do
            cards1 = cards1 + #ids
        end
        for _, ids in pairs(to.special_cards) do
            cards2 = cards2 + #ids
        end
        local n = math.abs(cards1-cards2)
        if player.room:askToSkillInvoke(player,{
            skill_name = shanzhi.name,
            prompt = "#ym1__shanzhi::"..to.id..":"..n
        }) then
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = room:getPlayerById(data.tos[1].id)
        local cards1,cards2 = 0,0
        for _, ids in pairs(player.special_cards) do
            cards1 = cards1 + #ids
        end
        for _, ids in pairs(to.special_cards) do
            cards2 = cards2 + #ids
        end
        local n = math.abs(cards1-cards2)
        --for i = 1, n, 1 do
            local choices, all_choices = {}, {"#ym1__shanzhi-choice1::"..player.id,"#ym1__shanzhi-choice2:::"..data.card:toLogString(),"#ym1__shanzhi-choice3::"..player.id}
            if not to:isNude() then
                table.insert(choices,"#ym1__shanzhi-choice1::"..player.id)
            end
            table.insert(choices,"#ym1__shanzhi-choice2:::"..data.card:toLogString())
            local targets = {}
            if #table.filter(room.alive_players, function (tp)
                if #tp:getPile("m_ex__zhonghui_power") > 0 then
                    table.insert(targets,tp)
                    return true
                end
            end) > 0 then
                table.insert(choices,"#ym1__shanzhi-choice3::"..player.id)
            end
            local result = room:askToChoices(to,{
                choices = choices,
                max_num = math.min(n,3,#choices),
                min_num = math.min(n,3,#choices),
                skill_name = shanzhi.name,
                all_choices = all_choices,
                cancelable = false,
            })
            local new_result = {}
            for _, v in ipairs(result) do
                table.insert(new_result,string.sub(v,21,21))
            end
            if table.contains(new_result,"1") then
                local card = room:askToCards(to,{
                    max_num = 1,
                    min_num = 1,
                    skill_name = shanzhi.name,
                    pattern = ".",
                    include_equip = true,
                    cancelable = false,
                    prompt = "#ym1__shanzhi-choice1::"..player.id
                })
                if #card > 0 then
                    room:moveCardTo(Fk:getCardById(card[1]), Card.PlayerHand, player, fk.ReasonGive, shanzhi.name, nil, false, target)
                end
            end
            if table.contains(new_result,"2") then
                data.additionalDamage = (data.additionalDamage or 0) + 1
            end
            if table.contains(new_result,"3") then
                local moves = room:askToChoosePlayers(player,{
                    targets = targets,
                    max_num = 1,
                    min_num = 1,
                    prompt = "#ym1__shanzhi-choice0",
                    skill_name = shanzhi.name,
                    cancelable = false,
                })
                if #moves > 0 then
                    local moveFrom = room:getPlayerById(moves[1].id)
                    local moveCards = room:askToChooseCards(player,{
                        target = moveFrom,
                        max = 1,
                        min = 1,
                        flag = {card_data = {{"权",moveFrom:getPile("m_ex__zhonghui_power")}}},
                        skill_name = shanzhi.name,
                        prompt = "#ym1__shanzhi-choice0",
                    })
                    if #moveCards then
                        local move = room:askToChoosePlayers(player,{
                            targets = table.filter(room.alive_players,function (p)
                                return p.id ~= moveFrom.id
                            end),
                            max_num = 1,
                            min_num = 1,
                            prompt = "#ym1__shanzhi-choice0",
                            skill_name = shanzhi.name,
                            cancelable = false,
                        })
                        if #move then
                            local moveTo = room:getPlayerById(move[1].id)
                            local quan = Fk:getCardById(moveCards[1])
                            --room:moveCardTo(quan, Card.PlayerSpecial, moveTo, fk.ReasonJustMove, shanzhi.name, "m_ex__zhonghui_power", true, player.id, nil, nil)
                            moveTo:addToPile("m_ex__zhonghui_power", quan, true, shanzhi.name)
                        end
                    end
                end
            end
        --end
    end,
})
return shanzhi